package com.noahplat.fence.controller;

/**
 * 功能：围栏控制层
 * name:宋彦磊
 * time:2021.03.08
 */

import com.noahplat.common.utils.Result;
import com.noahplat.fence.pojo.Fence;
import com.noahplat.fence.service.FenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/api/fence")
@Validated
public class FenceController {
    @Autowired
    private FenceService fenceService;

    /**
     * 增添围栏信息
     * 宋彦磊 2021.03.09
     *
     * @param fence
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-fence:FenceController:addFence')")
    @PostMapping("/addFence")
    public Result addFence(@RequestBody Fence fence,String[] imei) {
        return fenceService.addFence(fence,imei);
    }

    /**
     * 修改围栏信息
     * 宋彦磊 2021.03.09
     *
     * @param fence
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-fence:FenceController:updateFence')")
    @PutMapping("/updateFence")
    public Result updateFence(@RequestBody Fence fence) {
        return fenceService.updateFence(fence);
    }

    /**
     * 修改围栏删除标识，代表已删除围栏
     * 宋彦磊 2021.03.09
     *
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-fence:FenceController:delFence')")
    @PutMapping("/delFence")
    public Result delFence(@NotNull(message = "围栏ID不能为空") @Min(value = 1, message = "围栏ID不合法") Long id) {
        return fenceService.delFence(id);
    }

    /**
     * 查询未删除围栏信息
     * 宋彦磊 2021.03.09
     *
     * @param page
     * @param limit
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-fence:FenceController:getFence')")
    @GetMapping("/getFence")
    public Result getFence(@RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "10") Integer limit,@NotNull(message = "用户ID不能为空") @Min(value = 1, message = "用户ID不合法")Long uid, String imei) {
        return fenceService.getFence(page, limit,uid,imei);
    }

    /**
     * 查询未删除围栏信息(没有分页要求)
     * 宋彦磊 2021.03.09
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-fence:FenceController:getAppFence')")
    @GetMapping("getAppFence")
    public Result getAppFence(@NotNull(message = "用户ID不能为空") @Min(value = 1, message = "用户ID不合法")Long uid,String imei){
        return fenceService.getAppFence( uid, imei);
    }

    /**
     * 根据ID查询围栏的详细信息
     * 宋彦磊 2021.03.09
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-fence:FenceController:getFenceById')")
    @GetMapping("/getFenceById")
    public Result getId(@NotNull(message = "围栏ID不能为空") @Min(value = 1, message = "围栏ID不合法") Long id) {
        return fenceService.getFenceById(id);
    }

    /**
     * 修改围栏关联关系
     *
     * @param imei
     * @param newimei
     * @param fid
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-fence:FenceController:updateFencere')")
    @PutMapping("/updateFencere")
    public Result updateFencere(String[] imei, String[] newimei, @NotNull(message = "围栏ID不能为空") @Min(value = 1, message = "围栏ID不合法") Long fid) {
        return fenceService.updateFencere(imei, newimei, fid);
    }

    /**
     * 个人中心查询围栏
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-fence:FenceController:getUidFence')")
    @GetMapping("/getUidFence")
    public Result getUidFence(@RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "10") Integer limit,@NotNull(message = "用户ID不能为空 ") @Min(value = 1, message = "用户ID不合法") Long uid,String name){
        return fenceService.getUidFence(page, limit,uid,name);
    }

}
